<?php

namespace App\Models\Category;

use App\Models\Vocabulary\Vocabulary;

use App\Models\LearnWord\LearnWord;

use App\Models\Virtual\Virtual;

use App\Models\Sentence\Sentence;

use Illuminate\Database\Eloquent\Model;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Relations;
use Dcat\Admin\Traits\ModelTree;
use App\Models\Category\CategoryModule;
use App\Models\TextbookAudio\TextbookAudio;
use App\Models\Pdf\PdfImage;

class Category extends Model
{
    use HasDateTimeFormatter, ModelTree;

    protected $table = 'categorys';

    protected $fillable = [
        'name', 'image', 'parent_id', 'is_directory',
        'level', 'path', 'page', 'isbn',
        'unit_main_name', 'unit_name', 'module'
    ];

    protected $casts = [
        'is_directory' => 'boolean',
        'level' => 'integer',
        'page' => 'integer',
        'parent_id' => 'integer'
    ];

    // ModelTree 配置
    protected $titleColumn = 'name';
    protected $orderColumn = 'page';
    protected $parentColumn = 'parent_id';

    // 常量定义
    const SHOW_ON = 1;
    const SHOW_OFF = 0;

    public static array $isShowMap = [
        self::SHOW_ON => '显示',
        self::SHOW_OFF => '不显示',
    ];

    /**
     * 模型启动
     */
    protected static function boot()
    {
        parent::boot();

        static::saving(function (Category $category) {
            // 如果没有父级ID，设为顶级分类
            if (is_null($category->parent_id) || $category->parent_id == 0) {
                $category->level = 0;
                $category->parent_id = 0;
                $category->path = '-';
            } else {
                // 查找父级分类
                $parent = self::find($category->parent_id);
                if ($parent) {
                    $category->level = $parent->level + 1;
                    $category->path = $parent->path . $category->parent_id . '-';
                }
            }
        });
    }

    /**
     * 父级关系
     */
    public function parent(): Relations\BelongsTo
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }

    /**
     * 子级关系
     */
    public function children(): Relations\HasMany
    {
        return $this->hasMany(Category::class, 'parent_id');
    }

    /**
     * 获取所有后代
     */
    public function descendants()
    {
        return $this->children()->with('descendants');
    }

    /**
     * 获取树形选项
     */
    public static function selectOptions()
    {
        return self::orderBy('level')->orderBy('page')->get()->toTree()->pluck('name', 'id');
    }

    public function sentence(): Relations\HasMany
    {
        return $this->hasMany(Sentence::class);
    }

    public function virtual(): Relations\HasMany
    {
        return $this->hasMany(Virtual::class);
    }

    public function virtual_unit(): Relations\HasMany
    {
        return $this->hasMany(Virtual::class, 'unit_id', 'id');
    }

    public function learn_word(): Relations\HasMany
    {
        return $this->hasMany(LearnWord::class, 'grade_id');
    }

    public function vocabulary(): Relations\HasMany
    {
        return $this->hasMany(Vocabulary::class);
    }

    public function vocabulary_game(): Relations\HasMany
    {
        return $this->hasMany(Vocabulary::class)->where('type', Vocabulary::TYPE_GAME);
    }

    public function category_module(): Relations\HasMany
    {
        return $this->hasMany(CategoryModule::class, 'category_id');
    }

    public function textbook_audio(): Relations\HasMany
    {
        return $this->hasMany(TextbookAudio::class);
    }

    public function PdfImage(): Relations\HasMany
    {
        return $this->hasMany(PdfImage::class);
    }
}
